import { Canvas, instantiate, Prefab, UITransform ,Node, Layers, ResolutionPolicy, view, Size,screen} from "cc";
import { ResManager } from "../res/resManager";
import { EViewLayer } from "./EViewLayer";
import { Match } from "../../../GamePlay/modules/Match3/Match3";
import { BL } from "../res/ResConst";

function getUIClassBUrl(uiClass:any):IBundleUrl{
    if(uiClass === Match){
        return BL("prefab/match3","Match3BN")
    }

    console.log(`其它类型暂未处理`)
}

export const G_VIEW_SIZE = new Size(0, 0);
function adapterScreen() {
    let resolutionPolicy: ResolutionPolicy = view.getResolutionPolicy();
    let designSize = view.getDesignResolutionSize();

    let frameSize = screen.windowSize;

    let frameW = frameSize.width;
    let frameH = frameSize.height;

    //is too width
    const isScreenWidthLarger = (frameW / frameH) > (designSize.width / designSize.height)
    let targetResolutionPolicy = isScreenWidthLarger ? ResolutionPolicy.FIXED_HEIGHT : ResolutionPolicy.FIXED_WIDTH;

    if(targetResolutionPolicy !== resolutionPolicy.getContentStrategy().strategy){
         //保证设计分辨率都能显示
         view.setDesignResolutionSize(designSize.width,designSize.height,targetResolutionPolicy);
         view.emit("canvas-resize");
    }


    if(isScreenWidthLarger){
         G_VIEW_SIZE.width = Math.ceil(designSize.height * frameSize.width / frameSize.height);
         G_VIEW_SIZE.height = designSize.height;
    }else{
         G_VIEW_SIZE.width = designSize.width;
         G_VIEW_SIZE.height = Math.ceil(designSize.width * frameSize.height / frameSize.width);
    }

    console.log(`屏幕${isScreenWidthLarger ? "更宽, 高度适配" : "更高, 宽度适配"} 设计分辨率比例下的屏幕尺寸: ${G_VIEW_SIZE.width}x${G_VIEW_SIZE.height}`);
    return isScreenWidthLarger;
 }

class MyLayer{
    public readonly node:Node;

    constructor(public readonly layer:EViewLayer,public readonly canvas:Canvas,name:string){
        const node = this.node = new Node(name);
        node.layer = Layers.Enum.UI_2D;
        node.addComponent(UITransform);
        canvas.node.addChild(node);
    }
}


export class UiManager{

    private static _instance:UiManager = null;
    private m_Canvas:Canvas = null
    private m_Layers:MyLayer[] = []

    init(canvas: Canvas) {
        this.m_Canvas = canvas;
        adapterScreen();
        for (let layer = EViewLayer.Scene, maxLayer = EViewLayer.Toast; layer <= maxLayer; ++layer) {
            this.m_Layers.push(new MyLayer(layer, canvas, EViewLayer[layer]));
        }

    }

    static getInstance(){
        if(this._instance === null){
            this._instance = new UiManager();
        }

        return this._instance;
    }

    private constructor(){

    }

    open(uiClass:any ){
        const bUrl = getUIClassBUrl(uiClass);
        ResManager.getInstance().loadPrefabByBUrl(bUrl,prefab => {
            let match3Node = instantiate(prefab);
            // this.node.addChile(match3Node);
            this.m_Layers[EViewLayer.UI].node.addChild(match3Node);
            match3Node.getComponent(UITransform).setContentSize(G_VIEW_SIZE.clone());
        })
    }


}

